package com.mou.service.impl;

import com.mou.entity.Category;
import com.mou.entity.Transaction;
import com.mou.mapper.*;
import com.mou.service.*;
import com.mou.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service("homeService")
public class HomeServiceImpl implements HomeService {
    @Autowired
    private HomeMapper homeMapper;


    @Override
    public List<KpiDataVO> kpi() {
        List<KpiDataVO> kpiDataVOList = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.00"); // 用于保留两位小数

        // 获取今日成交额
        KpiDataVO TodayTurnover = new KpiDataVO();
        TodayTurnover.setLabel("今日成交额");

        // 今日和昨日日期
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);

        Double kpiToday = homeMapper.kpi(today);
        if(kpiToday ==null){
            TodayTurnover.setValue("0");
        }else {
            TodayTurnover.setValue(String.valueOf(kpiToday));
        }
        Double kpiYesterday = homeMapper.kpi(yesterday);
        if(kpiYesterday != null && kpiToday != null && kpiYesterday != 0){
            TodayTurnover.setTrend(String.valueOf((kpiToday - kpiYesterday)/(kpiYesterday*100)).substring(0,4));
        }else{
            TodayTurnover.setTrend("0");
        }
        kpiDataVOList.add(TodayTurnover);
        // 在线商品数
        KpiDataVO NumberItemsOnline = new KpiDataVO();
        NumberItemsOnline.setLabel("在线商品数");
        Integer CountToday = homeMapper.getNumberItemsOnline(today);
        NumberItemsOnline.setValue(CountToday != null ? String.valueOf(CountToday) : "0");

        Integer CountYesterday = homeMapper.getNumberItemsOnline(yesterday);
        if(CountYesterday != null && CountToday != null && CountYesterday != 0){
            double trendValue = (CountToday - CountYesterday) * 100.0 / CountYesterday;
            NumberItemsOnline.setTrend(df.format(trendValue));
        } else {
            NumberItemsOnline.setTrend("0.00");
        }
        kpiDataVOList.add(NumberItemsOnline);

        // 今日订单量
        KpiDataVO TodayOrder = new KpiDataVO();
        TodayOrder.setLabel("今日订单量");
        Integer CountTodayOrder = homeMapper.getTodayOrder(today);
        TodayOrder.setValue(CountTodayOrder != null ? String.valueOf(CountTodayOrder) : "0");

        Integer CountYesterdayOrder = homeMapper.getTodayOrder(yesterday);
        if(CountYesterdayOrder != null && CountTodayOrder != null && CountYesterdayOrder != 0){
            double trendValue = (CountTodayOrder - CountYesterdayOrder) * 100.0 / CountYesterdayOrder;
            TodayOrder.setTrend(df.format(trendValue));
        } else {
            TodayOrder.setTrend("0.00");
        }
        kpiDataVOList.add(TodayOrder);

        // 平台用户数（今日新增用户）
        KpiDataVO NumberUsers = new KpiDataVO();
        NumberUsers.setLabel("平台用户数");
        Integer CountUsersToday = homeMapper.getNumberUsers(today);
        NumberUsers.setValue(CountUsersToday != null ? String.valueOf(CountUsersToday) : "0");

        Integer CountUsersYesterday = homeMapper.getNumberUsers(yesterday);
        if(CountUsersYesterday != null && CountUsersToday != null && CountUsersYesterday != 0){
            double trendValue = (CountUsersToday - CountUsersYesterday) * 100.0 / CountUsersYesterday;
            NumberUsers.setTrend(df.format(trendValue));
        } else {
            NumberUsers.setTrend("0.00");
        }
        kpiDataVOList.add(NumberUsers);

        return kpiDataVOList;
    }

    @Override
    public TradeTrendDataVO tradeTrend() {
        LocalDate today = LocalDate.now();
        List<String> xData = new ArrayList<>();
        List<Double> series = new ArrayList<>();
        for (int i = 6; i >=0; i--) {
            xData.add(today.minusDays(i).toString().substring(5,10));
            Double kpiToday = homeMapper.kpi(today.minusDays(i));
            series.add(kpiToday != null ? kpiToday : 0);
        }
        TradeTrendDataVO tradeTrendDataVO = new TradeTrendDataVO();
        tradeTrendDataVO.setXData(xData);
        tradeTrendDataVO.setSeries(series);

        return tradeTrendDataVO;
    }

    @Override
    public List<CategoryDistributionVO> categoryDistribution() {
        List<CategoryDistributionVO> categoryDistribution = homeMapper.categoryDistribution();
        return categoryDistribution;
    }

    @Override
    public List<RealtimeTradesDataVO> realtimeTrades() {
        List<RealtimeTradesDataVO> realtimeTradesDataVOList = homeMapper.realtimeTrades();
        return realtimeTradesDataVOList;
    }
}
